package com.example.loopyserver.server.impl;


import com.example.loopyserver.common.ResultCode;
import com.example.loopyserver.dto.AnswerDTO;
import com.example.loopyserver.exception.CommonException;
import com.example.loopyserver.server.QAService;
import com.example.loopyserver.server.TaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


@Service
@RequiredArgsConstructor
public class QAServiceImpl implements QAService {
    private final static String QUESTION_TASK_TYPE = "answer";
    private final static int TASK_TIMEOUT_SECONDS = 10;
    private final static int WAIT_TIMEOUT_SECONDS = 20;
    private final TaskService taskService;

    @Override
    public AnswerDTO question(String text) throws CommonException {
        AnswerDTO res = taskService.pushAndWaitDone(
                QUESTION_TASK_TYPE,
                text,
                AnswerDTO.class,
                TASK_TIMEOUT_SECONDS,
                WAIT_TIMEOUT_SECONDS,
                null);
        if (res == null) {
            throw new CommonException(ResultCode.QA_TIME_OUT);
        }
        return res;
    }
}
